package atech.ace.mapping;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import atech.ace.mapping.persistence.entities.*;

public class Validation {
	public static String[] validateProject(Project proj){
		ArrayList<String> errs = new ArrayList<String>();
		if(proj.getProjectName() == null || proj.getProjectName().isEmpty())
			errs.add("Project name cannot be empty");
		if(proj.getLocation() == null || proj.getLocation().isEmpty() )
			errs.add("Project location cannot be empty");
		if(proj.getLatitude() < -180 || proj.getLatitude() > 180)
			errs.add("Project latitude not in valid range");
		if(proj.getLongitude() < -180 || proj.getLongitude() > 180)
			errs.add("Project longitude not in valid rang");
		if(proj.getCompany() == null)
			errs.add("Project company cannot be empty");
		if(proj.getHumanResources().size() < 1)
			errs.add("Project must have at least one resource");
		return  errs.toArray(new String[errs.size()]);
	}
	
	
	public static String[] validateCompany(Company comp){
		ArrayList<String> errs = new ArrayList<String>();
		if(comp.getName() == null || comp.getName().isEmpty())
			errs.add("Company name cannot be empty");
		return  errs.toArray(new String[errs.size()]);
	}
	
	public static String[] validateResource(HumanResource resc){
		ArrayList<String> errs = new ArrayList<String>();
		if(resc.getFirstName() == null || resc.getFirstName().isEmpty())
			errs.add("Resource first name cannot be empty");
		if(resc.getLastName() == null || resc.getLastName().isEmpty())
			errs.add("Resource last name cannot be empty");
		if(resc.getExpertise() == null || resc.getExpertise().isEmpty())
			errs.add("Resource expertise cannot be empty");
		if(resc.getRegion() == null || resc.getRegion().isEmpty())
			errs.add("Resource region cannot be empty");
		if(resc.getEmail() == null || resc.getEmail().isEmpty() )
			errs.add("Resource email cannot be empty");
		else if(!isValidEmailAddress(resc.getEmail()))
			errs.add("Resource email is not a valid email address");
		return  errs.toArray(new String[errs.size()]);
	}
	
	public static boolean isValidEmailAddress(String emailAddress){  
		String  expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";  
		CharSequence inputStr = emailAddress;  
		Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);  
		Matcher matcher = pattern.matcher(inputStr);  
		return matcher.matches();  
	}  
}
